<template>
	<view class="contain">
		<view class="course_contain"> 
			<image mode="widthFix" :src="course.poster"></image>
			<view class="course_name">{{course.name}}</view>
			<view style="font-size: 34rpx;margin-top: 40rpx;">班级列表</view>
			<view v-for="(item, index) in class_list" :key="index">
				<view class="course_list">
					<view class="class">{{item.name}}</view>
					<view v-if="item.price" class="setting" @click="changePop(index)">￥{{item.price}}</view>
					<view v-else class="setting" @click="changePop(index)">设置单节课价</view>
				</view>
				<view class="divider" v-if="index != class_list.length-1"></view>
			</view>
		</view>
		<!-- 显示弹窗 -->
		<form v-if="showPop" @submit="submit">
			<view class="popup" @click="hidePop()"></view>
			<view style="display: flex; justify-content: center;">
				<view class="popup_contain">
					<view>修改金额</view>
					<view class="contain_price">
						<text>￥</text>
						<input focus="true" type="digit" name="price" adjust-position="false" :value="class_list[selectIndex].price" />
						<button form-type="submit">确定</button>
					</view>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	import cloudbase from '@cloudbase/js-sdk';
	
	const db = cloudbase.database();
	const dbCmd = db.command;
	export default {
		data() {
			return {
				selectIndex: -1,
				showPop: false,
				course: {
					name: '车尼尔钢琴初级教程',
					img: 'https://img-blog.csdnimg.cn/20210316180124467.png'
				},
				class_list: []
			}
		},
		onLoad(e) {
			if(e.item){
				let course = JSON.parse(e.item);
				cloudbase.getTempFileURL({
					fileList: [course]
				}).then(res => {
					let list = res.fileList[0];
					this.course = list.tempFileURL ? list.tempFileURL : course;
				})
				this.courseList = res.data;
				this.getClass(course);
			}
			if(e.price){
				let item = JSON.parse(e.price);
				this.course.name = item.name;
				this.course.img = item.avatar;
				let className = item.name.split("初级")[0];
				this.class_list[0].name = className + '课';
			}
		},
		methods: {
			getClass: function(course){
				// 获取班级
				db.collection('class').where({
					institutionId: getApp().globalData.userInfo.institutionId,
					type: course.label
				}).get().then(res=>{
					res.data.forEach(classItem=>{
						classItem.courses.forEach(item=>{
							if(item._id == course._id){
								// console.log(classItem);
								let temp = {
									_id: classItem._id,
									name: classItem.name,
									price: classItem.price
								}
								this.class_list.push(temp);
							}
						})
					})
				})
			},
			changePop: function(index){
				this.showPop = true;
				console.log(index)
				this.selectIndex = index;
			},
			hidePop: function(){
				this.showPop = false;
			},
			submit: function(e){
				let price = e.detail.value.price;
				let index = this.selectIndex;
				this.class_list[index].price = price;
				this.showPop = false;
				// 设置该班级单次课的价格
				db.collection('class').where({
					_id: this.class_list[this.selectIndex]._id
				}).update({
					price: price
				}).then(res=>{
					if(res.updated == 1){
						uni.showToast({
							title: '修改成功',
							duration: 1000
						})
					}
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #F3F5F7;
	}
	.contain{
		margin: 10rpx 30rpx;
	}
	.course_contain{
		background-color: #fff;
		padding: 15rpx 30rpx;
		margin-top: 20rpx;
		margin-bottom: 60rpx;
		border-radius: 24rpx;
		display: flex;
		flex-direction: column;
		color: #666;
	}
	.course_contain image{
		height: 400rpx;
	}
	.course_name{
		margin: 10rpx 0rpx;
		font-size: 40rpx;
	}
	.course_list{
		display: flex;
		justify-content: space-between;
		margin: 20rpx 0rpx;
	}
	.class{
		font-size: 30rpx;
		color: #999;
	}
	.setting{
		font-size: 30rpx;
		color: #FD973F;
	}
	.divider {
		height: 2rpx;
		width: 100%;
		background-color: #eee;
	}
	.popup{
		width: 100%;
		height: 100%;
		position: fixed;
		background-color: rgba(0,0,0, 0.4);
		top: 0;
		left: 0;
		display: flex;
		justify-content: center;
	}
	
	.popup_contain{
		position: fixed;
		width: 80%;
		height: 80px;
		bottom: 50%;
		background-color: #fff;
		border-radius: 24rpx;
		z-index: 999;
		padding: 5%;
	}
	
	.contain_price{
		display: flex;
		border-bottom: #ddd solid 1rpx;
		padding-bottom: 10rpx;
		margin: 30rpx 0rpx;
		align-items: center;
	}
	
	.contain_price text{
		font-size: 55rpx;
		font-weight: bold;
		margin-right: 20rpx;
	}
	
	.contain_price button{
		background-color: #FD973F;
		color: #fff;
		height: 70rpx;
		font-size: 30rpx;
		margin-right: 0rpx;
		align-items: center;
	}
</style>
